home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 419_01 / odmg10 / lib / odmg / Ref.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-28  |  3.0 KB  |  208 lines

  1. /*******************************<+>***************************
  2.  **                             DTA
  3.  *************************************************************
  4.  **
  5.  **  $Id: Ref.h,v 1.6 1994/03/01 23:01:26 dta Exp $
  6.  **
  7.  **  $Source: /cvs/lib/odmg/Ref.h,v $
  8.  **
  9.  **  What @(#):
  10.  **
  11.  **  Author: Dale T. Anderson
  12.  **
  13.  *******************************<+>***************************/
  14.  
  15. #ifndef Ref_h
  16. #define Ref_h
  17.  
  18. #include <iostream.h>
  19. #include <assert.h>
  20.  
  21. typedef short Position;
  22. typedef int boolean;
  23.  
  24. #define FALSE 0
  25. #define TRUE !0
  26.  
  27. template <class T>
  28. class Ref
  29. {
  30.     T * m_ref;
  31.  
  32.     public:
  33.  
  34.     Ref (const T *ref = NULL);
  35.     Ref (const Ref <T> &);
  36.  
  37.     Ref <T> & operator = (T *);
  38.     Ref <T> & operator = (const Ref <T> &);
  39.  
  40.     T * operator -> (void);
  41.     const T * operator -> (void) const;
  42.  
  43.     operator T * ();
  44.     operator const T * ();
  45.  
  46.     boolean operator == (const Ref <T> &) const;
  47.     boolean operator == (const T *) const;
  48.     boolean operator != (const Ref <T> &) const;
  49.     boolean operator != (const T *) const;
  50.  
  51.     T &operator * ();
  52.     const T &operator * () const;
  53.     T &operator [] (int);
  54.  
  55.     virtual ~Ref (void);
  56.  
  57.     boolean is_deleted (void) const;
  58.     boolean is_active (void) const;
  59.  
  60.     void destroy (void);
  61.  
  62.     //
  63.     // Additions
  64.     //
  65.  
  66.     Ref (T &ref);
  67.  
  68.     void set_ref (T *ref);
  69. };
  70.  
  71. //
  72. // Methods
  73. //
  74.  
  75. template <class T>
  76. Ref <T>::Ref (const T *ref) :
  77.     m_ref ((T *) ref)
  78. {
  79. }
  80.  
  81. template <class T>
  82. Ref <T>::Ref (const Ref <T> &ref) :
  83.     m_ref (ref.m_ref)
  84. {
  85. }
  86.  
  87. template <class T>
  88. Ref <T> &Ref <T>::operator = (T * ref)
  89. {
  90.     m_ref = ref;
  91.     return *this;
  92. }
  93.  
  94. template <class T>
  95. Ref <T> &Ref <T>::operator = (const Ref <T> &ref)
  96. {
  97.     m_ref = ref.m_ref;
  98.     return *this;
  99. }
  100.  
  101. template <class T>
  102. T * Ref <T>::operator -> ()
  103. {
  104.     return (T *) m_ref;
  105. }
  106.  
  107. template <class T>
  108. const T * Ref <T>::operator -> () const
  109. {
  110.     return m_ref;
  111. }
  112.  
  113. template <class T>
  114. Ref <T>::operator T * ()
  115. {
  116.     return m_ref;
  117. }
  118.  
  119. template <class T>
  120. Ref <T>::operator const T * ()
  121. {
  122.     return m_ref;
  123. }
  124.  
  125. template <class T>
  126. T &Ref <T>::operator * ()
  127. {
  128.     return *m_ref;
  129. }
  130.  
  131. template <class T>
  132. const T &Ref <T>::operator * () const
  133. {
  134.     return *m_ref;
  135. }
  136.  
  137. template <class T>
  138. T &Ref <T>::operator[] (int index)
  139. {
  140.     return (m_ref[index]);
  141. }
  142.  
  143. template <class T>
  144. boolean Ref <T>::operator== (const Ref <T> &ref) const
  145. {
  146.     return (m_ref == ref.m_ref);
  147. }
  148.  
  149. template <class T>
  150. boolean Ref <T>::operator == (const T * ref) const
  151. {
  152.     return (m_ref == ref);
  153. }
  154.  
  155. template <class T>
  156. boolean Ref <T>::operator != (const Ref <T> &ref) const
  157. {
  158.     return (m_ref != ref.m_ref);
  159. }
  160.  
  161. template <class T>
  162. boolean Ref <T>::operator != (const T * ref) const
  163. {
  164.     return (m_ref != ref);
  165. }
  166.  
  167. template <class T>
  168. Ref <T>::~Ref()
  169. {
  170. }
  171.  
  172. template <class T>
  173. boolean Ref <T>::is_deleted() const
  174. {
  175.     return (m_ref == NULL);
  176. }
  177.  
  178. template <class T>
  179. boolean Ref <T>::is_active() const
  180. {
  181.     return (m_ref != NULL);
  182. }
  183.  
  184. template <class T>
  185. void Ref <T>::destroy()
  186. {
  187.     delete m_ref;
  188.     m_ref = NULL;
  189. }
  190.  
  191. //
  192. // Additions
  193. //
  194.  
  195. template <class T>
  196. Ref <T>::Ref (T &ref)
  197. {
  198.     m_ref = &ref;
  199. }
  200.  
  201. template <class T>
  202. void Ref<T>::set_ref (T *ref)
  203. {
  204.     m_ref = ref;
  205. }
  206.  
  207. #endif
  208.